﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;

namespace SalaryChannel.Web
{
    [ServiceContract(Namespace = "http://salary.yangchao.ts.gs.chinamobile/")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class TaskService: ServiceAuthRoot
    {
        [OperationContract]
        public List<SP_TaskStatus_个结果> GetTaskStatus(int TaskMonth)
        {
            // 在此处添加操作实现
            try
            {
                Authoricate();
                using (DataClassesQueryDataContext context = new DataClassesQueryDataContext())
                {
                    return context.SP_TaskStatus(TaskMonth).ToList();
                }
            }
            catch (Exception ex)
            {

                LogManager.Write("TaskService.GetTaskStatus:" + ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 启动任务
        /// </summary>
        /// <param name="TaskID"></param>
        /// <param name="TaskMonth"></param>
        /// <param name="UserID"></param>
        /// <returns></returns>
        [OperationContract]
        public bool StartTask(int TaskID, int TaskMonth, string UserID)
        {
            int ret = -999;
            try
            {
                Authoricate();
                using (DataClassesQueryDataContext context = new DataClassesQueryDataContext())
                {
                    ret = context.SP_StartTask(TaskID, TaskMonth, UserID);
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("TaskService.StartTask:" + ex.Message);
            }
            return ret == 0;
        }

        /// <summary>
        /// 获取任务流程
        /// </summary>
        /// <param name="TaskID"></param>
        /// <param name="TaskMonth"></param>
        /// <returns></returns>
        [OperationContract]
        public List<SP_TaskFlow_个结果> GetTaskFlow(int TaskID, int TaskMonth)
        {
            try
            {
                Authoricate();
                using (DataClassesQueryDataContext context = new DataClassesQueryDataContext())
                {
                    return context.SP_TaskFlow(TaskID, TaskMonth).ToList();
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("TaskService.GetTaskFlow:" + ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 获取我的任务
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        [OperationContract]
        public List<VW_TaskOn> GetMyJob(string UserID,int TaskMonth)
        {
            try
            {
                Authoricate();   
                using (DataClassesQueryDataContext context = new DataClassesQueryDataContext())
                {
                    var query = from q in context.VW_TaskOn
                                where q.User_ID == UserID
                                where q.Task_Month ==TaskMonth
                                orderby q.Start_Time 
                                select q;
                    if (query.Any())
                    {
                        return query.ToList();
                    }
                    return new List<VW_TaskOn>();
                }
            }
            catch (Exception ex)
            {
                 LogManager.Write("TaskService.GetMyJob:" + ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 获取渠道的上月新增及名称
        /// </summary>
        /// <param name="TaskMonth"></param>
        /// <param name="ChannelID"></param>
        /// <returns></returns>
        [OperationContract]
        public string GetChannelLastNewUser(int TaskMonth, string ChannelID)
        {
            try
            {
                Authoricate();
                using (DataClassesQueryDataContext context = new DataClassesQueryDataContext())
                {
                    var query = from a in context.ChannelAssignment
                                join c in context.ChannelAccount
                                on a.Channel_ID equals c.Id
                                where a.Statis_Month == TaskMonth && a.Channel_ID == ChannelID
                                select new { c.CountyCode,a.Last_Mon_New_Users, c.ChannelName };
                    if (query.Any())
                    {
                        return string.Format("[area]{0}[name]{1}[users]{2}",query.First().CountyCode,query.First().ChannelName,
                           query.First().Last_Mon_New_Users);
                    }
                    return string.Empty;
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("TaskService.GetChannelLastNewUser:" + ex.Message);
                return null;
            }
        }

        [OperationContract]
        public bool UpdateChannelAssign(int TaskMonth,string ChannelID, int Assign, string Remark)
        {
            try
            {
                Authoricate();
                using (DataClassesQueryDataContext context = new DataClassesQueryDataContext())
                {
                    var query = from a in context.ChannelAssignment
                                where a.Statis_Month == TaskMonth && a.Channel_ID == ChannelID
                                select a;
                    if (query.Any())
                    {
                        query.First().Assignment = Assign;
                        query.First().Remark = Remark;
                        context.SubmitChanges();
                        return true;
                    }
                    return false;
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("TaskService.UpdateChannelAssign:" + ex.Message);
                return false;
            }
        }

        [OperationContract]
        public bool GetIntoNextStep(int FlowID,string Remark)
        {
            try
            {
                Authoricate();
                using (DataClassesQueryDataContext context = new DataClassesQueryDataContext())
                {
                    return context.SP_GetIntoNextStep(FlowID, Remark) == 0;
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("TaskService.GetIntoNextStep:" + ex.Message);
                return false;
            }
        }

        [OperationContract]
        public bool GetBackToLastStep(int FlowID, string Remark)
        {

            try
            {
                Authoricate();
                using (DataClassesQueryDataContext context = new DataClassesQueryDataContext())
                {
                    return context.SP_GetBackToLastStep(FlowID, Remark) == 0;
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("TaskService.GetBackToLastStep:" + ex.Message);
                return false;
            }
        }

        [OperationContract]
        public List<string> GetChannelAssignment(int TaskMonth, string UserID)
        {
            List<string> list = new List<string>();
            try
            {
                Authoricate();
                using (DataClassesQueryDataContext context = new DataClassesQueryDataContext())
                {
                    var query = from u in context.VW_UserWithChannel
                                join c in context.ChannelAccount on u.CountyCode equals c.CountyCode
                                join a in context.ChannelAssignment on c.Id equals a.Channel_ID
                                where a.Statis_Month ==TaskMonth
                                where u.UserID == UserID
                                select new
                                {
                                    c.CountyCode,
                                    a.Channel_ID,
                                    c.ChannelName,
                                    a.Last_Mon_New_Users,
                                    a.Assignment,
                                    a.Remark
                                };
                    if (query.Any())
                    {
                        foreach (var a in query)
                        {
                            list.Add(string.Format("[area]{0}[id]{1}[name]{2}[last]{3}[assign]{4}[remark]{5}",
                                a.CountyCode, a.Channel_ID, a.ChannelName, a.Last_Mon_New_Users, a.Assignment,
                                a.Remark));
                        }
                    }
                    return list;
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("TaskService.GetBackToLastStep:" + ex.Message);
                return null;
            }
        }
       
    }
}
